//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../common/js/ajax.js'); //引入Axios方法
let WxValidate = require('../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		cur_id: 0,
		cur_menu: 0,
		rebackTk: false,
		evaluate: [],
		store_id: 0,
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this;
		this.initValidate()
		this.load = this.selectComponent("#loading");
		this.setData({
			store_id: e.id
		})
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		Axios.get(app.globalData.baseUrl + 'App/User/Evaluate/my_receive_evaluate', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			type: this.data.cur_menu,
			store_id: this.data.store_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					evaluate: data
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '评价管理'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	onReachBottom: function() {
		var _this = this;
		wx.showLoading({
			title: '玩命加载中',
		})
		if (switch_p) {
			switch_p = false
			page = page + 1;
			Axios.get(app.globalData.baseUrl + 'App/User/Evaluate/my_receive_evaluate?p=' + page, {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				owner: 1,
				type: this.data.cur_menu,
				store_id: this.data.store_id
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = _this.data.evaluate.concat(data)
					_this.setData({
						evaluate: temp
					})
					switch_p = true
					wx.hideLoading();
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleCloseTap: function() {
		this.setData({
			rebackTk: false
		})
	},
	handleEvtap: function(e) {
		var _this = this
		page = 1
		var id = e.currentTarget.dataset.id;
		_this.load.showIng()
		this.setData({
			cur_menu: id,
			evaluate: []
		})
		Axios.get(app.globalData.baseUrl + 'App/User/Evaluate/my_receive_evaluate', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			type: this.data.cur_menu,
			store_id: this.data.store_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					evaluate: data
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
	},
	handleRebTap: function(e) {
		var id = e.currentTarget.dataset.id;
		var type = e.currentTarget.dataset.type;
		if (type > 0) {
			this.setData({
				rebackTk: true,
				cur_id: id
			})
		}
	},
	handleComTap: function(e) {
		var id = e.currentTarget.dataset.id;
		wx.navigateTo({
			url: '/pages/shopEvaluate/evaluate/evaluate?id=' + id + '&store_id=' + this.data.store_id
		})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var _this = this
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
		}
		var obj = Object.assign(e, CONST);
		Axios.get(app.globalData.baseUrl + 'App/User/Evaluate/evaluate_reply',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info;
					_this.data.evaluate.forEach(function(item, key) {
						if (item.order_id == cur_id) {
							var temp = item.order[key].reply_msg
							temp.push(data)
							_this.setData({
								['item.order[' + key + '].reply_msg']: temp
							})
						}
					})

				} else {
					_this.showModal(res)
				}
			})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			order_id: {
				required: true,
			},
			content: {
				required: true,
				rangelength: [2, 100]
			}
		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			order_id: {
				required: '订单为空',
			},
			content: {
				required: '请输入回复内容',
				rangelength: '请输入2~100个汉字个汉字'
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
